package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyTotalAssetsAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.AllAccountInfo;
import com.kkd.kkdapp.http.entity.IncomeDetail;

import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.UIEmptyHandleUtil;
import com.kkd.kkdapp.view.KkdaiRoundProgressBar;

public class MyIncomeActivity extends BaseActivity implements OnClickListener {

	private final static int UPDATE_UI = 1;
	private final static int CODE_ELSE = 2;

	private Context mContext = this;
	private RelativeLayout rel_top_back_myIncome;
	private long totalIncome;
	private String myRank;
	private List<String> list_nameArr;
	private List<String> list_valueArr;
	private List<String> list_colors = new ArrayList<String>();
	private KkdaiRoundProgressBar round_myIncome;
	private TextView tv_tip_myIncome;
	private TextView tv_totalIncome_myIncome;
	private DecimalFormat df = new DecimalFormat("#,###.00");

	private IncomeDetail mIncomeDetail;
	private ListView listview;
	private List<String> tvArr = new ArrayList<String>();
	private List<String> colorArr = new ArrayList<String>();
	private List<String> valueArr = new ArrayList<String>();
	private RelativeLayout emptyView;
	private ScrollView nonUullView;
	private ImageView id_no_content;
	private TextView id_no_content_text;

	private Handler mhandle = new Handler() {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.Handler#handleMessage(android.os.Message)
		 */
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE_UI:
				UIEmptyHandleUtil.uiNullDispose(false, nonUullView, emptyView,
						id_no_content, id_no_content_text, "", 0);
				displayCacheData();
				break;
			case CODE_ELSE:
				int statusCode = (Integer) msg.obj;
				if (statusCode == KkdHttpClient.NO_NET) {
					UIEmptyHandleUtil.uiNullDispose(true, nonUullView,
							emptyView, id_no_content, id_no_content_text,
							"网络错误，请稍后重试", R.drawable.img_null_net);
				} else if (statusCode == KkdHttpClient.RESOLUTION_EXCEPTION) {
					UIEmptyHandleUtil.uiNullDispose(true, nonUullView,
							emptyView, id_no_content, id_no_content_text,
							"服务异常，请稍后重试", R.drawable.img_null_net);
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_income);
		initView();
		initListener();
		displayCacheData();
	}

	private void initView() {
		/*
		 * 返回键
		 */
		rel_top_back_myIncome = (RelativeLayout) findViewById(R.id.rel_top_back_myIncome);
		/*
		 * 总收益金额
		 */
		tv_totalIncome_myIncome = (TextView) findViewById(R.id.tv_totalIncome_myIncome);
		/*
		 * 超过了...
		 */
		tv_tip_myIncome = (TextView) findViewById(R.id.tv_tip_myIncome);

		listview = (ListView) this.findViewById(R.id.listview);
		listview.setDividerHeight(0);

		/*
		 * 初始化饼图
		 */
		round_myIncome = (KkdaiRoundProgressBar) findViewById(R.id.round_myIncome);
		round_myIncome.setRoundWidth(SystemUtils.dip2px(this, 25));
		emptyView = (RelativeLayout) this.findViewById(R.id.emptyView);
		nonUullView = (ScrollView) this.findViewById(R.id.nonUullView);
		id_no_content = (ImageView) this.findViewById(R.id.id_no_content);
		id_no_content_text = (TextView) this
				.findViewById(R.id.id_no_content_text);
	}

	private void displayCacheData() {
		String json = PreferenceUtils.getInstance().getMyproperty(this);
		LogUtil.e("responseString", json);
		if (json == null || "".equals(json)) {
			requestMyIncome();
		} else {

			Gson gson = new Gson();
			AllAccountInfo mAllAccountInfo = gson.fromJson(json,
					AllAccountInfo.class);
			mIncomeDetail = mAllAccountInfo.getAccHomeData().getIncomeDetail();
			round_myIncome.setPropressArray(mIncomeDetail.getiOSTotalIncome()
					.getValueArr());
			// 总收益
			totalIncome = mIncomeDetail.getTotalIncome();
			// 超过了。。。
			myRank = mIncomeDetail.getMyRank();
			// 添加名称
			list_nameArr = mIncomeDetail.getiOSTotalIncome().getNameArr();
			// 添加名称对应的值
			list_valueArr = mIncomeDetail.getiOSTotalIncome().getValueArr();
			// 添加名称对应的色值
			list_colors = mIncomeDetail.getiOSTotalIncome().getColorArr();
			/*
			 * 设置文字
			 */
			tv_totalIncome_myIncome.setText(df.format((totalIncome / 100f))
					.startsWith(".") ? "0" + df.format((totalIncome / 100f))
					: df.format((totalIncome / 100f)));
			if (tv_totalIncome_myIncome.getText().toString().length() > 9) {
				tv_totalIncome_myIncome.setTextSize(13f);
			} else {
				tv_totalIncome_myIncome.setTextSize(16f);
			}
			String numTip = "";
			if (myRank.contains("全国")) {
				numTip = myRank.substring(myRank.indexOf("国") + 1,
						myRank.indexOf("的"));
			} else {
				numTip = myRank;
			}
			tv_tip_myIncome.setText(numTip);
			// 设置标题
			for (int i = 0; i < list_colors.size(); i++) {

				tvArr.add(list_nameArr.get(i));
			}
			// 设置标题对应的值
			for (int i = 0; i < list_valueArr.size(); i++) {
				valueArr.add(list_valueArr.get(i));
			}
			// 设置标题对应的颜色
			for (int i = 0; i < list_colors.size(); i++) {
				colorArr.add(list_colors.get(i));
			}
			// 设置饼图的比例
			round_myIncome.setcolorArray(list_colors);
			MyTotalAssetsAdapter adapter = new MyTotalAssetsAdapter(this,
					tvArr, colorArr, valueArr);
			listview.setAdapter(adapter);

		}
	}

	private void initListener() {
		rel_top_back_myIncome.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		round_myIncome.initView(0);
		round_myIncome.invalidate();
	}

	/*
	 * 请求我的总收益
	 */
	private void requestMyIncome() {
		String customerIdAndToken = PreferenceUtils.getInstance()
				.getcustomerId(mContext);
		String[] cat = customerIdAndToken.split("&");
		if (cat == null || cat.length < 2)
			return;
		KkdHttpMagager.getInstance().getManageManager(new HttpCallBack() {

			@Override
			public void onSuccess(int statusCode, Object res) {
				mhandle.sendEmptyMessage(UPDATE_UI);

			}

			@Override
			public void onFailure(int statusCode, String responseBody) {
				Message msg = new Message();
				msg.obj = statusCode;
				msg.what = CODE_ELSE;
				mhandle.sendMessage(msg);
				// AppUtil.makeToastForCustomer(mContext, R.string.abnormal);
			}
		});
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.rel_top_back_myIncome:
			finish();
			break;

		default:
			break;
		}
	}
}
